package com.nabi.starter.common.base;


import com.nabi.starter.common.exception.ApiCode;
import com.nabi.starter.common.exception.ErrorCode;

import java.util.List;


public class ResultUtil {
    public static Result<String> ok() {
        return result(ErrorCode.OK);
    }

    public static <T> Result<T> result(T obj) {
        Result<T> result = new Result<T>(ErrorCode.OK);
        if (obj == null) {
            result.setMsg("暂无数据");
        }
        result.setResult(obj);
        return result;
    }

    public static <T> Result<List<T>> result(List<T> list) {
        Result<List<T>> result = new Result<List<T>>(ErrorCode.OK, list);
        result.setResult(list);
        return result;
    }

    public static <T> Result<List<T>> result(Long pageCurrent, Long pageTotal, Long pages, Long pageSize, List<T> list) {
        Result<List<T>> result = new Result<List<T>>(ErrorCode.OK, list);
        result.setPageTotal(pageTotal);
        result.setPages(pages);
        result.setPageCurrent(pageCurrent);
        result.setPageSize(pageSize);
        return result;
    }

    public static <T> Result<T> result(ErrorCode ErrorCode, String msg) {
        Result<T> result = new Result<T>(new ApiCode(ErrorCode.getCode(), msg));
        return result;
    }

    public static <T> Result<T> result(ErrorCode code) {
        Result<T> result = new Result<T>(code);
        return result;
    }

    public static <T> Result<T> ifResult(int batchs) {
        if (batchs > 0) {
            return result(ErrorCode.OK);
        } else {
            return result(ErrorCode.OPERATION_FAILURE);
        }
    }

    public static <T> Result<T> ifResult(boolean modify) {
        if (modify) {
            return result(ErrorCode.OK);
        } else {
            return result(ErrorCode.OPERATION_FAILURE);
        }
    }


}
